// pages/select_books/select_books.js
import Page from '../common/page';

var app = getApp();
const getBookList = app.require('config').getBookList;
const getSubjectList = app.require('config').getSubjectList;
const https = app.require('/utils/httpTools');

var lastindex = -1;

Page({
  data: {
    active: 0,
    show: false, //学段浮层
    booklist: [],
    subjectlist: [],
    publiclist:[{name:"人民教育出版社",short_name:"人教版"},{name:"山东教育出版社",short_name:"鲁教版"},{name:"外国语研sfasffafa究出版社",short_name:"外研社"},{name:"合唱大龙asfasfsdfasdf卷风设",short_name:"啊水电费"},{name:"啊啊放假垃圾分类",short_name:"安防的"}],
    showSelectPublic:false,
    scrollHight: app.data.windows_height,
  },

  onClosePublicPop(){
    this.setData({ showSelectPublic: false });
  },

  onOpenPublicPop(){
    this.setData({ showSelectPublic: true });
  },

  showPopup() {
    this.setData({ show: true });
  },

  onClose() {
    this.setData({ show: false });
  },

  getSubjectList(period_id) {
    var self = this;
    https.get(
      getSubjectList,
      {
        "period_id": period_id,
      },
      true
    ).then(rs => {
      // console.info(rs);
      var that = self;
      that.setData({ subjectlist: rs });
      that.getBookList(1, rs[0].subject_id, 3);
    }).catch(err => {
      console.log("error:", err);
      // 接口失败操作
      wx.showToast({
        title: err.errMsg || "服务异常，请稍后重试！",
        icon: 'error',
        duration: 2000
      })
    });
  },

  getBookList(period_id, subject_id, publish_id) {
    console.log('获取教材subject_id=' + subject_id);
    var self = this;
    https.get(
      getBookList,
      {
        "period_id": period_id,
        "subject_id": subject_id,
        "publish_id": publish_id,
      },
      true //缓存
    ).then(rs => {
      // console.info(rs);
      var that = self;
      that.setData({ booklist: rs });
      //加载当前选择教材
      lastindex = 2;
      //更新数据集显示selected
      this.refreshSelectedBtn(2);
    }).catch(err => {
      console.log("error:", err);
      // 接口失败操作
      wx.showToast({
        title: err.errMsg || "服务异常，请稍后重试！",
        icon: 'error',
        duration: 2000
      })
    });
  },

  refreshSelectedBtn(index) {
    var newlist = this.data.booklist;
    newlist[lastindex].selected = false;
    newlist[index].selected = true;
    lastindex = index;
    this.setData({
      booklist: newlist,
    });
  },

  onSelectBook(event) {
    this.refreshSelectedBtn(event.currentTarget.dataset.idx);
  },

  onChangeTabs(event) {
    // {index: 1, name: 1, title: "数学"}
    console.info(event.detail);
    // 加载对应数据
    console.info(this.data.subjectlist[event.detail.index].subject_name);
    this.getBookList(1, this.data.subjectlist[event.detail.index].subject_id, 3);
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getSubjectList(3);
    var temp = this.data.publiclist;
    temp[2].isSelectPub = true;
    this.setData({
      publiclist : temp,
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})